﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

using System.Net;
using System.Xml;
using System.Reflection;
using System.Diagnostics;
using System.IO;

namespace ExtendedEventsManager
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {

            try  // Attempt to connect to the internet and check the build number.
            {
                Properties.Settings s = Properties.Settings.Default;
                if (s.CheckVersion)
                    CheckVersionInfo();
            }
            catch 
            {
               
            }

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new Main());
            Application.Run(new MainForm());
/*
            try
            {
                Application.Run(new MainForm());
            }
            catch (Exception ex)
            {
                MessageBox.Show("An unhandled exception occured and has been logged. Please post the contents of the log on the codeplex site.", "An Unexpected Error has Occured!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                TextWriter tw = new StreamWriter("ErrorLog.txt", true);
                tw.WriteLine();
                tw.WriteLine("******************************** BEGIN EXCEPTION ********************************");
                tw.WriteLine("Application Version   : "+Assembly.GetExecutingAssembly().GetName().Version.ToString());
                tw.WriteLine();
                LogError(tw, ex);
                tw.WriteLine("********************************* END EXCEPTION ********************************");
                tw.Flush();
                tw.Close();
            }
 */
        }

        private static void LogError(TextWriter tw, Exception ex)
        {

            tw.WriteLine("HelpLink:");
            tw.WriteLine(ex.HelpLink);
            tw.WriteLine("Message:");
            tw.WriteLine(ex.Message);
            tw.WriteLine("Source:");
            tw.WriteLine(ex.Source);
            tw.WriteLine("StackTrace:");
            tw.WriteLine(ex.StackTrace);
            tw.WriteLine();
            if (ex.InnerException != null)
            {
                LogError(tw, ex);
            }
        }

        private static void CheckVersionInfo()
        {
            WebClient wc = new WebClient();
            XmlDocument xDoc = new XmlDocument();
            string s = wc.DownloadString(@"http://www.sqlclr.net/XEventsLatestBuild.xml");
            xDoc.LoadXml(s);
            string[] buildarray = xDoc.DocumentElement.Attributes["number"].Value.Split('.');

            int major = Int32.Parse(buildarray[0]),
                minor = Int32.Parse(buildarray[1]),
                build = Int32.Parse(buildarray[2]),
                revision = Int32.Parse(buildarray[3]);

            if (Assembly.GetExecutingAssembly().GetName().Version.Major < major ||
                Assembly.GetExecutingAssembly().GetName().Version.Minor < minor ||
                Assembly.GetExecutingAssembly().GetName().Version.Build < build ||
                Assembly.GetExecutingAssembly().GetName().Version.Revision < revision)
            {
                DialogResult dr = MessageBox.Show(@"A Newer Version of the Extended Events Manager is now available! 

You are currently running version   : "+Assembly.GetExecutingAssembly().GetName().Version.ToString()+@"
The latest release version is            : "+xDoc.DocumentElement.Attributes["number"].Value+@"

Please visit http://www.codeplex.com/ExtendedEventManager and download the latest release.  

By clicking 'Yes' below, you can open this site in your web browser automatically.", "New version available!", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

                if (dr == DialogResult.Yes)
                {
                    Process.Start("iexplore.exe", "http://www.codeplex.com/ExtendedEventManager");
                }
            }
        }
    }
}
